﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NJL.WebUsers.Controls
{
    public partial class WUCIndexSearch : BaseUserControl
    {
        private readonly BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        private readonly BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        private readonly BLL.NJL_ATTRACTION bllAttraction = new BLL.NJL_ATTRACTION();
        private readonly BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        private readonly BLL.NJL_USER bllUser = new BLL.NJL_USER();
        private readonly BLL.NJL_LOCAL_COLOR bllLocalColor = new BLL.NJL_LOCAL_COLOR();

        protected int PROVINCE_COUNT = 0;
        protected int CITY_COUNT = 0;
        protected int COMPANY_COUNT = 0;
        protected int USER_COUNT = 0;
        protected int LOCAL_COLOR_COUNT = 0;
        protected int ATTRACTION_COUNT = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtKeywords.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + ibtnSearch.ClientID + "').click();return false;}} else {return true}; ");
                LoadData();
            }
        }
        private void LoadData()
        {
            cblServiceType.DataSource = bllServiceType.GetAllList();
            cblServiceType.DataValueField = "TYPE_CODE";
            cblServiceType.DataTextField = "TYPE_NAME";
            cblServiceType.DataBind();

            //PROVINCE_COUNT = bllArea.GetModelList(" deep=1 ").Count;
            CITY_COUNT = bllArea.GetModelList(" deep=2 and AREA_CODE like '" + WEBSITE_CODE.Substring(0, 2) + "%' ").Count;
            COMPANY_COUNT = bllCompany.GetListCount(" NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' ");
            //USER_COUNT = bllUser.GetModelList(" NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' ").Count;
            USER_COUNT = bllUser.GetModelList(" ").Count;
            LOCAL_COLOR_COUNT = bllLocalColor.GetModelList(" NJL_LOCAL_COLOR.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' ").Count;
            ATTRACTION_COUNT = bllAttraction.GetModelList(" NJL_ATTRACTION.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' ").Count;
        }
        protected void ibtnSearch_Click(object sender, EventArgs e)
        {

            string strValue = "";
            for (int i = 0; i < cblServiceType.Items.Count; i++)
            {
                if (cblServiceType.Items[i].Selected)
                {
                    if (strValue == "")
                    {
                        strValue = cblServiceType.Items[i].Value;
                    }
                    else
                    {
                        strValue += "," + cblServiceType.Items[i].Value;
                    }
                }
            }

            string strKey = txtKeywords.Text.Trim();
            if (strKey == "试试输入你喜欢的活动，快速找到最好玩的！")
            {
                strKey = "";
            }

            if (strValue == "")
            {
                Response.Redirect("/SearchList.aspx?cc=" + WEBSITE_FOCUS_CITY_NAME_PINYIN + "&key=" + Server.UrlEncode(strKey), true);
            }
            else
            {
                Response.Redirect("/SearchList.aspx?cc=" + WEBSITE_FOCUS_CITY_NAME_PINYIN + "&st=" + strValue + "&key=" + Server.UrlEncode(strKey), true);
            }
        }

    }
}